package com.bcn.login_mybatisplus_demo.pay.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

import com.bcn.login_mybatisplus_demo.interceptor.AuthStorage;
import com.bcn.login_mybatisplus_demo.pay.domain.TAppPay;
import com.bcn.login_mybatisplus_demo.pay.mapper.TAppPayMapper;
import com.bcn.login_mybatisplus_demo.pay.service.ITAppPayService;
import com.bcn.login_mybatisplus_demo.util.Convert;
import com.bcn.login_mybatisplus_demo.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 日常账本Service业务层处理
 *
 * @author administrator
 * @date 2025-03-11
 */
@Service
public class TAppPayServiceImpl implements ITAppPayService
{
    @Autowired
    private TAppPayMapper tAppPayMapper;

    /**
     * 查询日常账本
     *
     * @param payId 日常账本ID
     * @return 日常账本
     */
    @Override
    public TAppPay selectTAppPayById(Long payId)
    {
        return tAppPayMapper.selectTAppPayById(payId);
    }

    /**
     * 查询日常账本列表
     *
     * @param tAppPay 日常账本
     * @return 日常账本
     */
    @Override
    public List<TAppPay> selectTAppPayList(TAppPay tAppPay)
    {
        tAppPay.setUserId(AuthStorage.getUser().getId());
        return tAppPayMapper.selectTAppPayList(tAppPay);
    }

    @Override
    public List<HashMap<String,Object>> selectTAppPayListByDay(TAppPay tAppPay) {

        tAppPay.setUserId(AuthStorage.getUser().getId());
        List<String> strings = tAppPayMapper.selectDay(tAppPay);

        List<HashMap<String,Object>> list = new ArrayList<>();
        HashMap<String, HashMap<String,Object>> stringObjectHashMap = new HashMap<>();

        // 支出
        double zzc = 0;
        // 收入
        double zsr = 0;
        String title = "";
        // 返回每日数据
        List<TAppPay> tAppPays = new ArrayList<>();
        if (strings != null && strings.size() > 0) {
            for (String string : strings) {
                HashMap<String, Object> stringObjectHashMap1 = new HashMap<>();
                tAppPays = tAppPayMapper.selectTAppPayListByDay(string,AuthStorage.getUser().getId());
                if (tAppPays != null && tAppPays.size() > 0) {
                    for (TAppPay appPay : tAppPays) {
                        if (appPay.getPayType().equals("0")){
                            zsr += appPay.getPayAmount().doubleValue();
                        }else {
                            zzc -= appPay.getPayAmount().doubleValue();
                        }

                        String date = DateUtils.getDate();
                        Calendar instance = Calendar.getInstance();
                        instance.add(Calendar.DAY_OF_MONTH, -1);
                        Date time = instance.getTime();
                        String format = new SimpleDateFormat("yyyy-MM-dd").format(time);
                        if (appPay.getPayAmountDate().equals(date)){
                            title = "今天";
                        }else if (appPay.getPayAmountDate().equals(format)){
                            title = "昨天";
                        }else {
                            title = appPay.getPayAmountDate();
                        }

                        stringObjectHashMap1.put("title", title);
                    }
                }
                stringObjectHashMap1.put("zzc", zzc);
                stringObjectHashMap1.put("zsr", zsr);
                stringObjectHashMap1.put("tAppPays",tAppPays);
                list.add(stringObjectHashMap1);
            }
        }


//        tAppPayMapper.selectTAppPayList(tAppPay);

        return list;
    }

    /**
     * 新增日常账本
     *
     * @param tAppPay 日常账本
     * @return 结果
     */
    @Override
    public int insertTAppPay(TAppPay tAppPay)
    {
        tAppPay.setCreateTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        tAppPay.setUserId(AuthStorage.getUser().getId());
        return tAppPayMapper.insertTAppPay(tAppPay);
    }

    /**
     * 修改日常账本
     *
     * @param tAppPay 日常账本
     * @return 结果
     */
    @Override
    public int updateTAppPay(TAppPay tAppPay)
    {
        return tAppPayMapper.updateTAppPay(tAppPay);
    }

    /**
     * 删除日常账本对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteTAppPayByIds(String ids)
    {
        return tAppPayMapper.deleteTAppPayByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除日常账本信息
     *
     * @param payId 日常账本ID
     * @return 结果
     */
    @Override
    public int deleteTAppPayById(Long payId)
    {
        return tAppPayMapper.deleteTAppPayById(payId);
    }
}
